package com.ioi.base;

import java.io.Serializable;
import java.util.List;

/**
 * R 通用数据返回对象
 *
 * @author LILIBO
 * @since 2021-06-24
 */
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 默认数据量
     */
    public static final int DEFAULT_COUNT = 0;

    /**
     * 成功状态码
     */
    public static final int CODE_SUCCESS = 0;

    /**
     * 失败状态码
     */
    public static final int CODE_FAILURE = 1;

    /**
     * 成功消息
     */
    public static final String MSG_SUCCESS = "success";
    /**
     * 失败消息
     */
    public static final String MSG_FAILURE = "failure";

    /**
     * 返回状态码
     */
    private int code;

    /**
     * 返回消息
     */
    private String msg;

    /**
     * 返回数据量
     */
    private int count;

    /**
     * 返回数据
     */
    private T data;

    public R() {
    }

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(int code, String msg, int count, T data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

    public R<T> code(int code) {
        this.code = code;
        return this;
    }

    public R<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public R<T> count(int count) {
        this.count = count;
        return this;
    }

    public R<T> data(T data) {
        this.data = data;
        return this;
    }

    /**
     * 返回成功
     *
     * @return
     */
    public static <T> R<T> success() {
        return success(CODE_SUCCESS, null);
    }

    /**
     * 返回成功
     *
     * @param data
     * @return
     */
    public static <T> R<T> success(T data) {
        return success(MSG_SUCCESS, data);
    }

    /**
     * 返回成功
     *
     * @param msg
     * @param data
     * @return
     */
    public static <T> R<T> success(String msg, T data) {
        int count = data instanceof List<?> ? ((List<?>) data).size() : DEFAULT_COUNT;
        return new R<>(CODE_SUCCESS, msg, count, data);
    }

    /**
     * 返回成功
     *
     * @param count
     * @param data
     * @return
     */
    public static <T> R<T> success(int count, T data) {
        return new R<>(CODE_SUCCESS, MSG_SUCCESS, count, data);
    }

    /**
     * 返回失败
     *
     * @return
     */
    public static <T> R<T> failure() {
        return new R<>(CODE_FAILURE, MSG_FAILURE, DEFAULT_COUNT, null);
    }

    /**
     * 返回失败
     *
     * @param msg 返回消息
     * @return
     */
    public static <T> R<T> failure(String msg) {
        return failure(msg, null);
    }

    /**
     * 返回失败
     *
     * @param code 返回状态码
     * @param msg 返回消息
     * @return
     */
    public static <T> R<T> failure(int code, String msg) {
        return new R<>(code, msg, DEFAULT_COUNT, null);
    }

    /**
     * 返回失败
     *
     * @param data
     * @return
     */
    public static <T> R<T> failure(T data) {
        return failure(MSG_FAILURE, data);
    }

    /**
     * 返回失败
     *
     * @param msg 返回消息
     * @param data 返回数据
     * @return
     */
    public static <T> R<T> failure(String msg, T data) {
        int count = data instanceof List<?> ? ((List<?>) data).size() : DEFAULT_COUNT;
        return new R<>(CODE_FAILURE, msg, count, data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
